<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" top="20vh" width="450px" @close='close'>
    <div class="dialogContentBoxMaxHeight60">
      <el-form ref="form" :model="record" label-width="120px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('目的IPv6地址')" prop="ipv6_address">
          <el-input v-emoji v-model="record.ipv6_address" ref="ipv6Address" style="width:215px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('前缀长度')" prop="prefix_len">
          <el-input v-emoji v-model="record.prefix_len" ref="prefixlen" style="width:215px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('网口')" prop="interface">
          <el-select v-model="record.interface" ref="interface" :placeholder="$t('请选择出接口')" clearable filterable size='small'>
            <el-option v-for="item in policyList" :disabled="item.enPortMode === 3" :key="item.strPhysicsName" :label="item.strPhysicsName" :value="item.strPhysicsName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('下一跳')" prop="nexthop">
          <el-input v-emoji v-model="record.nexthop" ref="nexthop" style="width:215px;" size='small'></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addIpv6 } from '@/api/network/highAvailability/hotMachine'
import { getPhysicalApiArray } from '@/api/network/api/physical'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'Ipv6Dialog',
  data () {
    var validateInterface = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    var validateIpv4 = (rule, value, callback) => {
      let regexp = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (!regexp.test(value)) {
        callback(new Error(this.$t('请输入正确的ip地址')))
      } else {
        callback()
      }
    }
    var validateMask = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) <= 128 && Number(value) >= 0) {
        callback()
      } else {
        callback(new Error(this.$t('请输入0-128的数字')))
      }
    }
    return {
      dialogVisible: false,
      record: {
        ipv6_address: '',
        prefix_len: '',
        interface: '',
        nexthop: ''
      },
      recordCopy: {
        ipv6_address: '',
        prefix_len: '',
        interface: '',
        nexthop: ''
      },
      policyList: [],
      rules: {
        ipv6_address: [{ required: false, validator: validateIpv4, trigger: 'blur' }],
        interface: [{ required: false, validator: validateInterface, trigger: 'blur' }],
        prefix_len: [{ required: false, validator: validateMask, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init () {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['ipv6Address', 'prefixlen', 'nexthop']
        const selectData = ['interface']
        inputClass(this.$refs, data)
        selectClass(this.$refs, selectData)
      })
      getPhysicalApiArray().then(res => {
        if (res.status === 0) {
          this.policyList = res.info
        }
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          addIpv6(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('添加成功'),
                type: 'success'
              })
              this.close()
              this.$parent.hotMachineHandleClick()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.policyList = []
      this.$refs.form.resetFields()
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    }
  }
}
</script>
